unit interfacedesign;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, Buttons, ComCtrls, clientdb, configbutn;

type

  TButtonArray = array of TConfigButton;

  TButtonData = record
    Rect: TRect;
    ItemID: Integer;
    Visible: Boolean;
  end;

   { TInterfaceDesignForm }

  TInterfaceDesignForm = class ( TForm )
    ChangesBtn: TBitBtn;
    AddPageButton: TButton;
    EditButtonButton: TButton;
    DelPageButton: TButton;
    PageVisibleCheckBox: TCheckBox;
    PagesCombo: TComboBox;
    WidthEdit: TEdit;
    HeightEdit: TEdit;
    PageLabel: TLabel;
    WidthLabel: TLabel;
    HeightLabel: TLabel;
    PagePanel: TPanel;
    Panel2: TPanel;
    procedure ChangesBtnClick ( Sender: TObject ) ;
    procedure EditButtonButtonClick ( Sender: TObject ) ;
    procedure PagesComboSelect ( Sender: TObject ) ;
    procedure FormCreate ( Sender: TObject ) ;
    procedure FormPaint ( Sender: TObject ) ;
  private
    { private declarations }
    fPageNum: Integer;
    PagesDB: TBaseDB;
    ButtonsDB: TBaseDB;
    Buttons: array of TButtonArray;
    SelectedButton: TConfigButton;
    procedure SelectButton(Sender: TObject);
    procedure ChangeButtonDetails(Sender: TObject);
  public
    { public declarations }
    function RefreshPages: Integer;
    procedure GetButtons(pageid: Integer);
    procedure ClearButtons;
    procedure SendChanges;
  end;

var
  InterfaceDesignForm: TInterfaceDesignForm;

implementation

uses main, setitem;

{ TInterfaceDesignForm }

procedure TInterfaceDesignForm.FormCreate ( Sender: TObject ) ;
begin
  PagesDB := TBaseDB.Create;
  PagesDB.Tablename := 'Pages';
  ButtonsDB := TBaseDB.Create;
  ButtonsDB.Tablename := 'Buttons';
  MainDB.SQL := 'SELECT * FROM Buttons;';
  MainDB.Open;
  ButtonsDB.Assign(MainDB);
  MainDB.Close;
end;

procedure TInterfaceDesignForm.FormPaint ( Sender: TObject ) ;
begin
  PagePanel.Canvas.Rectangle(5,5,40,40);
end;

procedure TInterfaceDesignForm.SelectButton ( Sender: TObject ) ;
var
  x: Integer;
begin
  for x := 0 to ComponentCount-1 do
    if (Components[x] is TConfigButton) and (Components[x]<>Sender)
       then TConfigButton(Components[x]).Selected := false;
  ButtonsDB.RecNo := TConfigButton(Sender).Tag;
  {$IFDEF DEBUG} WriteLn('Selected ', TConfigButton(Sender).ItemID); {$ENDIF}
  SelectedButton := TConfigButton(Sender);
end;

procedure TInterfaceDesignForm.ChangeButtonDetails ( Sender: TObject ) ;
begin
  if TConfigButton(Sender).Tag=0 then
     begin
       ButtonsDB.Insert;
       ButtonsDB.FieldByName('row').AsInteger := TConfigButton(Sender).Row;
       ButtonsDB.FieldByName('col').AsInteger := TConfigButton(Sender).Col;
       TConfigButton(Sender).Tag := ButtonsDB.RecNo;
     end;
  if TConfigButton(Sender).ButtonVisible
     then ButtonsDB.FieldByName('visible').AsInteger := 1
     else ButtonsDB.FieldByName('visible').AsInteger := 0;
  ButtonsDB.FieldByName('itemid').AsInteger := TConfigButton(Sender).ItemID;
  ButtonsDB.FieldByName('modified').AsInteger := 1;
  ButtonsDB.Post;
end;

procedure TInterfaceDesignForm.ChangesBtnClick ( Sender: TObject ) ;
begin
  ButtonsDB.RecNo := 10;
  ShowMessage(PagesDB.ExportRecordAsSQL(sqlmInsert));
    ShowMessage(ButtonsDB.ExportRecordAsSQL(sqlmUpdate));

  //SendChanges;
end;

procedure TInterfaceDesignForm.EditButtonButtonClick ( Sender: TObject ) ;
var
  itemid: Integer;
  s: TListItem;
  id: Integer;
  x: Integer;
begin
  SetItemForm := TSetItemForm.Create(nil);
  if ButtonsDB.RecNo>-1 then
     begin
       itemid := ButtonsDB.FieldByName('itemid').AsInteger;
       x := 0;
       if itemid<1000 then
          begin
            SetItemForm.SelectPageControl.PageIndex := 0;
            with SetItemForm.ServicesList do
                 while (x<SetItemForm.ServicesList.Items.Count) and (itemid<>Integer(SetItemForm.ServicesList.Items[x].Data)) do
                        Inc(x);
            if x<SetItemForm.ServicesList.Items.Count then
               begin
                 SetItemForm.ServicesList.ItemIndex := x;
                 SetItemForm.ServicesList.Items[x].MakeVisible(true);
               end;
          end
       else
          begin
            SetItemForm.SelectPageControl.PageIndex := 1;
            with SetItemForm.ProductTree do
                 while (x<SetItemForm.ProductTree.Items.Count) and (itemid<>Integer(SetItemForm.ProductTree.Items[x].Data)) do
                  begin
                    {$IFDEF DEBUG} writeln(itemid, '*', Integer(SetItemForm.ProductTree.Items[x].Data)); {$ENDIF}
                    Inc(x);
                  end;
            if x<SetItemForm.ProductTree.Items.Count then
               begin
                 SetItemForm.ProductTree.Items[x].Selected := true;
                 SetItemForm.ProductTree.Items[x].MakeVisible;
               end;
          end;
     end;
  try
    if SetItemForm.ShowModal=mrOK then
       begin
         case SetItemForm.SelectPageControl.PageIndex of
              0: if SetItemForm.ServicesList.ItemIndex>-1 then
                 begin
                   x := SetItemForm.ServicesList.ItemIndex;
                   SelectedButton.ItemID := Integer(SetItemForm.ServicesList.Items[x].Data);
                   SelectedButton.ButtonLabel := SetItemForm.ServicesList.Items[x].Caption;
                   ChangeButtonDetails(SelectedButton);
                 end;

              1: if SetItemForm.ProductTree.SelectionCount>0 then
                 begin
                   SelectedButton.ItemID := Integer(SetItemForm.ProductTree.Selected.Data);
                   SelectedButton.ButtonLabel := SetItemForm.ProductTree.Selected.Text;
                   ChangeButtonDetails(SelectedButton);
                 end;
         end;
       end;
  finally
    SetItemForm.Free;
  end;
end;

procedure TInterfaceDesignForm.PagesComboSelect ( Sender: TObject ) ;
begin
  SendChanges;
  if PagesCombo.ItemIndex>-1
     then GetButtons(PagesCombo.ItemIndex);
  fPageNum := PagesCombo.ItemIndex;
end;

function TInterfaceDesignForm.RefreshPages: Integer;
var
  ix: Integer;
begin
  ix := PagesCombo.ItemIndex;
  MainDB.SQL := 'SELECT * FROM Pages ORDER BY id';
  MainDB.Open;
  PagesDB.Assign(MainDB);
  PagesDB.Fields[0].ReadOnly := true;
  PagesDB.Fields[0].IsIndexField := true;
  MainDB.Close;
  PagesCombo.Items.Clear;
  While not PagesDB.EOF do
        begin
          PagesCombo.Items.Add(PagesDB.FieldByName('title').AsString);
          PagesDB.Next;
        end;
  Result := 0;
  if (ix<0) or (ix>=PagesCombo.Items.Count)
     then PagesCombo.ItemIndex := 0
     else PagesCombo.ItemIndex := ix;
  fPageNum := PagesCombo.ItemIndex;
end;

procedure TInterfaceDesignForm.GetButtons ( pageid: Integer ) ;
var
  x: Integer;
  buttonwidth: Integer;
  B: TConfigButton;
  buttonheight: Integer;
  r: Integer;
  c: Integer;
  rows: Integer;
  cols: Integer;
  selectedset: Boolean;
  selectedrec: Integer;
begin
  MainDB.SQL := 'SELECT 0 as modified, itemid, row, col, visible, name FROM Buttons, Products WHERE Products.productid=Buttons.itemID AND pagenum='
                + IntToStr(pageid) + ';';
  MainDB.Open;
  ButtonsDB.Assign(MainDB);
  MainDB.Close;
  ButtonsDB.RecNo := 0;
  for x := 0 to ButtonsDB.RecordCount-1 do
    begin
      {$IFDEF DEBUG} WriteLn(ButtonsDB.ExportRecordAsSQL(sqlmInsert)); {$ENDIF}
      ButtonsDB.Next;
    end;
  PagesDB.RecNo := pageid;
  ClearButtons;
  rows := PagesDB.FieldByName('rows').AsInteger;
  cols := PagesDB.FieldByName('cols').AsInteger;
  buttonwidth := ((PagePanel.Width-10) div cols) - 10;
  buttonheight := ((PagePanel.Height-10) div rows) - 10;
  SetLength(Buttons, rows);
  selectedset := false;
  selectedrec := -1;
  for r := 0 to rows-1 do
    begin
      SetLength(Buttons[r], cols);
      for c := 0 to cols-1 do
        begin
          B := TConfigButton.Create(self);
          B.Width := buttonwidth;
          B.Height := buttonheight;
          B.Left := ((buttonwidth+10) * c) + 10;
          B.Top := ((buttonheight+10) * r) + 10;
          B.Row := r;
          B.Col := c;
          B.Parent := PagePanel;
          B.ButtonVisible := false;
          Buttons[r,c] := B;
          B.OnSelect := @SelectButton;
          B.OnVisibilityChanged := @ChangeButtonDetails;
        end;
    end;
  ButtonsDB.First;
  While not ButtonsDB.EOF do
        begin
          r := ButtonsDB.FieldByName('row').AsInteger ;
          c := ButtonsDB.FieldByName('col').AsInteger ;
          if (r<rows) and (c<cols) then
             begin
               B := Buttons[r,c];
               B.ButtonLabel := ButtonsDB.FieldByName('name').AsString;
               B.ItemID := ButtonsDB.FieldByName('itemid').AsInteger;
               B.OnVisibilityChanged := nil;
               B.ButtonVisible := ButtonsDB.FieldByName('visible').AsInteger<>0;
               B.OnVisibilityChanged := @ChangeButtonDetails;
               B.Tag := ButtonsDB.RecNo;
             end;
          if not selectedset then
             if B.ButtonVisible then
                begin
                  B.Selected := true;
                  selectedset := true;
                  selectedrec := ButtonsDB.RecNo;
                end;
          ButtonsDB.Next;
        end;
  ButtonsDB.RecNo := selectedrec;
end;

procedure TInterfaceDesignForm.ClearButtons;
var
  r, c: Integer;
  B: TConfigButton;
begin
  for r := 0 to Length(Buttons)-1 do
    for c := 0 to Length(Buttons[r])-1 do
    if Buttons[r, c]<>nil  then
       begin
         B := Buttons[r, c];
         B.Free;
         Buttons[r, c] := nil;
       end;
end;

procedure TInterfaceDesignForm.SendChanges;
var
  r: String;
  c: String;
  SQL: String;
begin
  SQL := '';
  ButtonsDB.First;
  while not ButtonsDB.EOF do
   begin
     if ButtonsDB.FieldByName('modified').AsInteger>0 then
        begin
          r := ButtonsDB.FieldByName('row').AsString;
          c := ButtonsDB.FieldByName('col').AsString;
          SQL := SQL + 'DELETE FROM buttons WHERE pagenum=' + IntToStr(fPageNum) + ' AND row=' + r + ' AND col=' + c + ';' + LineEnding;
          SQL := SQL + 'INSERT INTO buttons VALUES (' + ButtonsDB.FieldByName('itemID').AsString + ', '
                                                      + IntToStr(fPageNum) + ','
                                                      + ButtonsDB.FieldByName('row').AsString + ','
                                                      + ButtonsDB.FieldByName('col').AsString + ','
                                                      + ButtonsDB.FieldByName('visible').AsString + ');';
          ButtonsDB.FieldByName('modified').AsInteger := 0;
        end;
     ButtonsDB.Next;
   end;
 if Length(SQL)>0 then
     try
         if not MainDB.ExecRemoteSQL( SQL, true )
            then ShowMessage('Error when editing buttons')
            else ShowMessage('Button settings updated');
       finally
         MainDB.Close;
       end;
end;

{$R *.lfm}

end.

